/**
 * 将样式配置，结果维度数据映射关系，数据转换为echart的options
 * @param styleOptions
 * @param relativeConfig
 * @param data
 */
function template(title,styleOptions,relativeConfig,data){

    let fieldConfig = {legend:'',data:[]};

    for(let index in relativeConfig){

        let relationItem = relativeConfig[index];

        if(relationItem.relationDimension == 'legend' ){

            fieldConfig.legend = relationItem.resultColumn;
        }else if(relationItem.relationDimension == 'data' ){
            fieldConfig.data.push(relationItem.resultColumn);
        }
    }




    let legendKey = fieldConfig.legend['columnField'];

    let legendDesc = fieldConfig.legend['columnDesc'];

    let dataKeys = fieldConfig.data;


    let series=[];

    for(let index in dataKeys){

        series.push({
            name:legendDesc,
            field:dataKeys[index]['columnField'],
            name:dataKeys[index]['columnDesc'],
            type:'pie',
            data:[]
        });
    }

    let legendData =[];

    for(let index in data){

        legendData.push(data[index][legendKey]);
    }

    for( let index in data){

        let dataitem = data[index];

        for(let sindex in series){

            let serie = series[sindex];

            serie.data.push({
                name:dataitem[legendKey],
                value:dataitem[serie.field]});
        }

    }


    let option = {
        title : {
            text: title,
            x:'center'
        },
        tooltip : {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            type: 'scroll',
            orient: 'vertical',
            right: 10,
            top: 20,
            bottom: 20,
            data: legendData,

        },
        series :series
    };




    //处理样式

    let styleFunc = function (p,styleItem) {

        let key_l1 = styleItem.key;

        if(! p[key_l1]) {
            p[key_l1] = {};
        }


        if(! Array.isArray(p[key_l1] )){

            //如果不是数组  就直接配置样式

            if(styleItem.type != 'form'){

                p[key_l1] = styleItem.value;
            }else{

                for(let styleItem_l2 of styleItem.items){


                    styleFunc(p[key_l1],styleItem_l2);

                }

            }

        }else{

            //是数组的话 就将item配置样式




            for(let arrayItem of p[key_l1]){


                if(styleItem.type != 'form'){

                    arrayItem = styleItem.value;

                }else{

                    for(let styleItem_l2 of styleItem.items){

                        styleFunc(arrayItem,styleItem_l2);

                    }

                }

            }
        }




    }

    if(Array.isArray(styleOptions) ){

        for(let item of styleOptions){

            styleFunc(option,item);
        }

    }




    return option;

}